package com.gxhmplan.news.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.gxhmplan.news.dao.NewsDao;
import com.gxhmplan.news.entity.News;
import com.gxhmplan.user.service.NameException;
import com.gxhmplan.util.Util;

@Service("newsService")
public class NewsServiceImpl implements NewsService{

	@Resource
	NewsDao newsDao;
	@Resource
	NewsService newsService;
	
	public List<Map<String, Object>> findNews(Integer classify) {
		
		return newsDao.findNews(classify);
	}

	public String uploadPhoto(MultipartFile file, HttpServletRequest request) {
		String id = Util.reqGetId(request);
		if (file == null) {
			throw new NameException("文件不存在");
		}
		if (id == null || id.trim().isEmpty()) {
			throw new NameException("用户不存在");
		}
		return Util.upload(file, id, "NewsPhoto", request);
	}

	public int addNews(News news) {
		news.setId(UUID.randomUUID().toString());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		news.setCreateTime(sdf.format(new Date()));
		news.setUpdateTime(sdf.format(new Date()));
		news.setBrowse(0);
		return newsDao.addNews(news);
	}

	public int updateNews(String id) {
		News news = newsDao.findNewsByNewsId(id);
		Integer browse=news.getBrowse();
		if(null == browse){
			browse=0;
		}
		news.setBrowse(browse+1);
		return newsDao.updateNews(news);
	}

}
